package com.xintai.sparepart.spareandpart.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xintai.common.constant.Constants;
import com.xintai.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xintai.sparepart.spareandpart.mapper.BeiProductsMapper;
import com.xintai.sparepart.spareandpart.domain.BeiProducts;
import com.xintai.sparepart.spareandpart.service.IBeiProductsService;

/**
 * 备品备件货品Service业务层处理
 *
 * @author xintai
 * @date 2020-12-23
 */
@Service
public class BeiProductsServiceImpl implements IBeiProductsService
{
    @Autowired
    private BeiProductsMapper beiProductsMapper;

    /**
     * 查询备品备件货品
     *
     * @param proId 备品备件货品ID
     * @return 备品备件货品
     */
    @Override
    public BeiProducts selectBeiProductsById(Long proId)
    {
        return beiProductsMapper.selectBeiProductsById(proId);
    }

    /**
     * 查询备品备件货品列表
     *
     * @param beiProducts 备品备件货品
     * @return 备品备件货品
     */
    @Override
    public List<BeiProducts> selectBeiProductsList(BeiProducts beiProducts)
    {
        return beiProductsMapper.selectBeiProductsList(beiProducts);
    }

    /**
     * 新增备品备件货品
     *
     * @param beiProducts 备品备件货品
     * @return 结果
     */
    @Override
    public int insertBeiProducts(BeiProducts beiProducts)
    {
        beiProducts.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        beiProducts.setProTime(new Date());
        return beiProductsMapper.insertBeiProducts(beiProducts);
    }

    /**
     * 修改备品备件货品
     *
     * @param beiProducts 备品备件货品
     * @return 结果
     */
    @Override
    public int updateBeiProducts(BeiProducts beiProducts)
    {
        return beiProductsMapper.updateBeiProducts(beiProducts);
    }

    /**
     * 批量删除备品备件货品
     *
     * @param proIds 需要删除的备品备件货品ID
     * @return 结果
     */
    @Override
    public int deleteBeiProductsByIds(Long[] proIds)
    {
        Map<String, Object> map = new HashMap<>();
        map.put("proIds",proIds);
        map.put("proIsdel",Constants.FAIL);
        return beiProductsMapper.deleteBeiProductsByIds(map);
    }

    /**
     * 删除备品备件货品信息
     *
     * @param proId 备品备件货品ID
     * @return 结果
     */
    @Override
    public int deleteBeiProductsById(Long proId)
    {
        return beiProductsMapper.deleteBeiProductsById(proId, Constants.FAIL);
    }

    /**
     * 去重
     * @return
     */
    @Override
    public boolean selectProductsnamesake(BeiProducts beiProducts) {
        if (beiProductsMapper.selectProductsnamesake(beiProducts.getProName(),beiProducts.getTypeId())>0){
            return true;
        }
        return false;
    }

    /**
     * 审核与修改
     * @param beiProducts
     * @return
     */
    @Override
    public int checkProduct(BeiProducts beiProducts) {
        //审核时间
        beiProducts.setProChecktime(new Date());
        //审核状态0未通过，1通过
        beiProducts.setProFlag(1L);
        //审核人Id
        beiProducts.setProCheckid(SecurityUtils.getLoginUser().getUser().getUserId());
        return beiProductsMapper.checkProduct(beiProducts);
    }
}
